<template>
  <div class='home-container'>
    <!-- 导航栏 -->
    <van-nav-bar id="page-nav-bar" class="page-nav-bar" fixed>
      <van-button slot="title" size="small" icon="search" type="info" round to="/search">搜索</van-button>
    </van-nav-bar>
    <!-- tab栏 频道列表 -->
    <van-tabs v-model="active" animated swipeable>
      <van-tab :title="item.name" v-for="item in channelList" :key="item.id">
        <article-list :channel="item"></article-list>
      </van-tab>
      <!-- 占位符充当内容填充区域 -->
      <div slot="nav-right" class="placeholder-box"></div>
      <div slot="nav-right" class="hamburger-box" @click="isShowEdit = true">
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹窗 -->
    <van-popup v-model="isShowEdit" position="bottom" :style="{ height: '100%' }" closeable
      close-icon-position="top-left" class="edit-channel-popup">
      <channel-edit :my-channels="channelList" :active="active" @update-active="onUpdateActive" />
    </van-popup>
  </div>
</template>
<script>
import { getUserChannels } from '@/api/channel.js';
import ArticleList from './components/article-list.vue';
import ChannelEdit from './components/channel-edit.vue';
import { getItem } from '@/utils/storage';
import { mapState } from 'vuex';
export default {
  name: 'HomePage',
  components: {
    ArticleList,
    ChannelEdit,
  },
  props: {},
  data() {
    return {
      active: 0,
      channelList: [],
      isShowEdit: false,
    };
  },
  created() {
    this.getAllChannelList();
  },
  computed: {
    ...mapState(['user']),
  },
  methods: {
    getAllChannelList() {
      // 判断是否登录
      if (this.user) {
        // 已登录
        getUserChannels()
          .then((res) => {
            this.channelList = res.channels;
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        // 未登录
        // 判断本地存储是否有数据
        if (getItem('TOUTIAO_CHANNEL')) {
          // 有数据
          this.channelList = getItem('TOUTIAO_CHANNEL');
        } else {
          // 无数据
          getUserChannels().then((res) => {
            this.channelList = res.channels;
          });
        }
      }
    },
    onUpdateActive(index, isShowEdit = true) {
      this.active = index;
      this.isShowEdit = isShowEdit;
    },
  },
};
</script>
<style lang='less' scoped>
.home-container {
  margin-top: 174px; // 抵消导航栏个菜单栏高度
  margin-bottom: 100px; // 抵消底部导航栏高度
  /deep/ .page-nav-bar {
    .van-nav-bar__title {
      max-width: unset;
    }
    .van-button {
      width: 555px;
      height: 64px;
      border: none;
      font-size: 28px;
      background-color: #5babfb;
      .van-icon-search {
        font-size: 32px;
      }
    }
  }
  /deep/ .van-tabs {
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
    }
    .van-tab--active {
      color: #333333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
    .placeholder-box {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
    .hamburger-box {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      display: flex;
      justify-content: center;
      align-items: center;
      &:before {
        content: '';
        width: 1px;
        height: 100%;
        position: absolute;
        left: 0;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
      .icon-gengduo {
        font-size: 33px;
      }
    }
  }
  .edit-channel-popup {
    padding-top: 100px;
    box-sizing: border-box;
  }
}
</style>
